<template>
	<view>
		<view class="collect-list">
			<view class="collect-item" v-for="(item,index) in collectList" :key="index" >
				<image :src="item.imgUrl" class="colloect-pic" @click="toGoodsDetail(item.productId)"></image>
				<view class="title">
					{{item.productName}}
				</view>
				<view class="price">
					￥{{item.price}}
				</view>
				<view class="cancle" @click="cancleCollect(item)">
					取消收藏
				</view>
			</view>
		</view>
		
		<view class="none" v-if="!collectList.length">
			暂无收藏
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	export default {
		data() {
			return {
				collectList: [] // 收藏商品列表
			};
		},
		onLoad(option) {
			this.getCollectList(option.id)
		},
		methods: {
			// 查看商品详情
			toGoodsDetail(id){
				uni.navigateTo({
					url:'/subpkg/goods-detail/goods-detail?id='+id
				})
			},
			// 获取收藏商品列表
			async getCollectList(id) {
				const {
					data: res
				} = await uni.$http.post('/user/getUserCollectionList', {
					userId: this.userInfo._id,
					offset: 1,
					limit: 10
				})
				console.log(res.data.list)
				this.collectList = res.data.list
				this.collectList = this.collectList.filter(item=>{
					return item.state=='1'
				})
			},
			// 取消收藏
			async cancleCollect(goods) {
				console.log(goods)
				const {
					data: res
				} = await uni.$http.post('/product/collectionProduct', {
					userId: this.userInfo._id,
					productId: goods.productId
				})
				this.getCollectList()
				// console.log(res)
			}

		},
		computed: {
			...mapState('m_user', ['userInfo'])
		}

	}
</script>

<style lang="scss">
	.none{
		padding-left: 25rpx;
		color: #cecece;
	}
	.collect-list {
		display: flex;
		justify-content: space-between;
		padding: 0 24rpx;
		flex-wrap: wrap;

		.collect-item {
			// height: 500rpx;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			margin-top: 45rpx;

			.colloect-pic {
				width: 342rpx;
				height: 342rpx;
				vertical-align: middle;
			}

			.title {
				font-size: 28rpx;
				font-weight: 500;
				color: #333333;
				height: 75rpx;
				width: 340rpx;
				overflow: hidden;
				text-overflow: ellipsis;
				text-align: center;
				margin-top: 20rpx;
			}

			.price {
				font-size: 30rpx;
				font-weight: 400;
				color: #FF1111;
				margin-bottom: 10rpx;
			}

			.cancle {
				width: 130rpx;
				height: 38rpx;
				line-height: 38rpx;
				border: 1rpx solid #E60505;
				border-radius: 19rpx;
				font-size: 22rpx;
				font-weight: 400;
				color: #E60505;
				text-align: center;

			}
		}
	}
</style>
